package com.cheyun.netsalev3.view

import android.content.Intent
import android.graphics.Typeface
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.databinding.FragmentHomeTaskBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.view.home.ItemFragment
import com.cheyun.netsalev3.viewmodel.HomeTaskViewModel
import com.google.android.material.tabs.TabLayout


class HomeTaskFragment3 : BaseFragment<FragmentHomeTaskBinding>() {

    companion object {
        fun newInstance(from: String = "") =
                HomeTaskFragment3().apply {
                    arguments = Bundle().apply {
                        putString("from", from)
                    }
                }
    }

    private var from: String = ""
    lateinit var holder: ViewHolder
    override fun initContentView(): Int {
        return R.layout.fragment_home_task
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            from = it.getString("from")
        }
    }

    private var listFragmet = ArrayList<Fragment>()
    var titles = arrayOf("邀约客户", "到店客户")

    override fun initData() {

    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
    }

    override fun initViewModel(): ViewModel? {
        val factory = InjectorUtils.getHomeTaskViewModelFactory()
        return ViewModelProviders.of(this, factory).get(HomeTaskViewModel::class.java)
    }


    override fun initView() {
        listFragmet = ArrayList()
        listFragmet.add(ItemFragment.newInstance(1))
        listFragmet.add(ItemFragment.newInstance(2))
        val pagerAdapter = HomePageAdapter(childFragmentManager, listFragmet)
        binding.viewPager.adapter = pagerAdapter
        binding.tabLayout.setupWithViewPager(binding.viewPager)
        binding.tabLayout.setSelectedTabIndicator(R.drawable.shape_reverse_triangle)
        for (i in 0 until binding.tabLayout.tabCount) {
            var view = layoutInflater.inflate(R.layout.home_tab_item, null)
            holder = ViewHolder(view)
            binding.tabLayout.getTabAt(i)?.customView = view
            holder.tvTabName.apply {
                text = titles[i]
                when (i) {
                    0 -> {
                        setTextColor(context!!.resources.getColor(R.color.home_bottom_ser))
                        typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                    }
                    1 -> {
                        setTextColor(context!!.resources.getColor(R.color.data_day_nor))
                        typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
                    }
                }
            }
            if (i == 0) {
                //设置分割线
                val linearLayout = binding.tabLayout.getChildAt(0) as LinearLayout
                linearLayout.showDividers = LinearLayout.SHOW_DIVIDER_MIDDLE
                linearLayout.dividerDrawable = ContextCompat.getDrawable(context!!,
                        R.drawable.divider) //设置分割线的样式
            }
        }
        var loginData = MySharedPreferences.getLoginData()
        //tab选中的监听事件
        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                tab.customView?.apply {
                    holder = ViewHolder(this)
                    holder.tvTabName.setTextColor(context!!.resources.getColor(R.color.home_bottom_ser))
                    holder.tvTabName.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                }
                //让Viewpager跟随TabLayout的标签切换
                binding.viewPager.currentItem = tab.position
                if (tab.position == 1) {
                    var showAdd = false
                    loginData?.member?.apps?.receive?.apply {
                        for (i in this) {
                            if (i == 3) {
                                showAdd = true
                            }
                        }
                    }
                    loginData?.channel?.settings?.receive?.apply {
                        showAdd = this != 1
                    }
                    if(showAdd){
                        binding.addBut.visibility = View.VISIBLE
                    }else{
                        binding.addBut.visibility = View.GONE
                    }
                } else {
                    binding.addBut.visibility = View.GONE
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                tab.customView?.apply {
                    holder = ViewHolder(this)
                    holder.tvTabName.setTextColor(context!!.resources.getColor(R.color.data_day_nor))
                    holder.tvTabName.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
                }
            }

            override fun onTabReselected(tab: TabLayout.Tab) {

            }
        })

        binding.addBut.setOnClickListener {
            var intent = Intent(context, EditCustomStoreActivity::class.java)
            intent.putExtra("hiddenbind",true)
            startActivity(intent)
        }

        binding.lyscan.visibility = View.GONE

        if(from.equals("home")) {
            loginData?.member?.apps?.receive?.apply {
                for (i in this) {
                    //6 扫码权限
                    if (i == 6) {
                        binding.lyscan.visibility = View.VISIBLE
                    }
                }
            }
            loginData?.member?.isadmin?.apply {
                if (this == 1) {
                    binding.lyscan.visibility = View.VISIBLE
                }
            }
        }
    }

    inner class ViewHolder(tabView: View) {
        var tvTabName: TextView = tabView.findViewById(R.id.textview) as TextView
    }

}
